package com.kavenka.zhufengfm.app.fragments;


import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kavenka.zhufengfm.app.R;
import com.kavenka.zhufengfm.app.adapter.DiscoverFragmentPagerAdapter;
import com.kavenka.zhufengfm.app.fragments.discover.*;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment implements TabLayout.OnTabSelectedListener {


    private ViewPager viewPager;

    public DiscoverFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        TabLayout tabBar= (TabLayout) view.findViewById(R.id.discover_tabBar);
        tabBar.setTabGravity(TabLayout.GRAVITY_CENTER);

        viewPager = (ViewPager) view.findViewById(R.id.discover_viewPager);
        if (tabBar != null) {
            TabLayout.Tab tab = tabBar.newTab();
            tab.setText("推荐");
            tabBar.addTab(tab);

            tab = tabBar.newTab();
            tab.setText("分类");
            tabBar.addTab(tab);

            tab = tabBar.newTab();
            tab.setText("直播");
            tabBar.addTab(tab);

            tab = tabBar.newTab();
            tab.setText("榜单");
            tabBar.addTab(tab);

            tab = tabBar.newTab();
            tab.setText("主播");
            tabBar.addTab(tab);

            tabBar.setOnTabSelectedListener(this);

            List<Fragment> fragments=new LinkedList<Fragment>();
            fragments.add(new DiscoverCategoryFragment());
            fragments.add(new DiscoverRecommendFragment());
            fragments.add(new DiscoverPlayingFragment());
            fragments.add(new DiscoverListFragment());
            fragments.add(new DiscoverAnchorFragment());

            viewPager.setAdapter(new DiscoverFragmentPagerAdapter(getChildFragmentManager(), fragments));

            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabBar));
        }
        return view;
    }


    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int position = tab.getPosition();
        viewPager.setCurrentItem(position);
    }
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }
    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
